/**
 * Created by zhoulin on 2019/4/3.
 */
var companyId = sessionStorageUtils.getJson(storageKey.sessionStorageKey.companyId);
var schoolId = sessionStorageUtils.getJson(storageKey.sessionStorageKey.schoolId);
var schoolMap = sessionStorageUtils.getJson(storageKey.sessionStorageKey.schoolMap);
//表格初始化配置
var tableInit = function () {
    var columns = [
        {
            title: '全选',
            field: 'select',
            //复选框
            checkbox: true,
            width: 25,
            align: 'center',
            valign: 'middle'
        },
        {
            field: 'id',
            title: 'ID',
            align: 'center'
        },
        {
            field: 'nickname',
            title: '使用者',
            align: 'center'
        },
        {
            field: 'wxTel',
            title: '使用者电话',
            align: 'center'
        },
        {
            field: 'score',
            title: '积分',
            align: 'center'
        },
        {
            field: 'logType',
            title: '记录类别',
            align: 'center',
            formatter: function (value, row, index) {
                if (value == 1) {
                    return '<span class="label label-primary">获得</span>';
                } else if (value == -1) {
                    return '<span class="label label-danger">使用</span>';
                } else {
                    return '-';
                }
            }
        },
        {
            field: 'type',
            title: '积分类别',
            align: 'center',
            formatter: function (value, row, index) {
                if (!empty(value)) {
                    var val = typeMap.get(value);
                    if (empty(val)) {
                        return value;
                    }
                    return val;
                } else {
                    return value;
                }
            }
        },
        {
            field: 'gmtCreate',
            title: '创建时间',
            align: 'center',
            formatter: function (value, row, index) {
                return formatDateTime(value);
            }
        },
        {
            field: 'gmtModified',
            title: '更新时间',
            align: 'center',
            visible: false,
            formatter: function (value, row, index) {
                return formatDateTime(value);
            }
        }
    ];

    //请求服务数据时所传参数
    function queryParams(params) {
        return {
            pageNumber: params.pageNumber,
            pageSize: params.pageSize,
            compayId:companyId,
            schoolId:schoolId,
            type: $("#type").val(),
            logType: $("#logType").val(),
            searchText: params.searchText,
        }
    }

    var tableConfig = $.extend(bootstrapTableConfig(_HOST + _API.marketing_integralUserLog_list, queryParams, columns), {
        //这里是其他的一些个性化的 bootstrap-table options
        //例如 覆盖一些公共的配置
        //例如 添加一些私有的配置
        showColumns: true,//列显示操作按钮
        ajaxOptions: { //设置自定义请求头
            headers: {
                'Authorization': sessionStorageUtils.getJson(storageKey.sessionStorageKey.token),
                'permCoding': 'integralConfig:list'
            }
        },
        //例如格式化数据
        responseHandler: function (res) {
            //在ajax获取到数据，渲染表格之前，修改数据源
            //console.log(res)
            if (res.success) {
                //更新本地token
                sessionStorageUtils.setJsonStr(storageKey.sessionStorageKey.token, res.token);
            }
            return res;
        }
    });
    //调用实例化
    $('#bootstrapTableEvents').bootstrapTable(tableConfig);
    $(".search input").attr('placeholder', '输入名称搜索');
};

var app = new Vue({
    el: '#app',
    data: {
        selects: [],
    },
    //进入页面初始化方法
    created: function () {
        var that = this;
        var token = sessionStorageUtils.getJson(storageKey.sessionStorageKey.token);
        if (empty(token)) {
            layer.msg("<span style='color:red'>登录超时！请重新登录！</span>", {time: 1500, icon: 5});
            setTimeout(function () {
                window.location.href = _LOGIN_PAGE;
            }, 1200);

            return;
        }

    },
    methods: {
        selectStatus: function () {
            $('#bootstrapTableEvents').bootstrapTable('refresh');
        },
        // 得到字典map
        getDicList: function (keyTable) {
            var that = this;
            var ret = new Map();
            that.selects = [{keyS: "", keyValue: "积分类别"}];
            Get(that, _HOST + _API.common_dic_dicList + "?keyTable=" + keyTable)
                .then(function (response) {
                    // 响应成功回调
                    console.log(response);
                    if (response) {
                        if (response.success) {
                            that.selects = that.selects.concat(response.list);
                            var list = response.list;
                            for (var i = 0; i < list.length; i++) {
                                ret.set(parseInt(list[i].keyS), list[i].keyValue);
                            }
                        } else {
                            layer.msg(response.msg, {time: 1500, icon: 5});
                        }
                    }
                });
            return ret;
        },
    },
});

var typeMap = app.getDicList('integralType');

$(function () {
    //初始化表格
    tableInit();
});
